﻿using System;
using System.Windows.Forms;
using DesignerHelper.EventHandlers;
using DesignerHelper.Help;
using SpreadsheetGear;
using SpreadsheetGear.Commands;
using WorkbookDesigner.Main;
using spForm = SpreadsheetGear.Windows.Forms;
using DesignerHelper.Main;

namespace WorkbookDesigner.UI
{
    internal partial class InsertCellsDialog : Form
    {
        private static InsertCellsDialog Icd { get; set; }
        private IMainWindow Mw { get; set; }
        private spForm.WorkbookView ActiveView { get; set; }


        internal static void ShowStaticDialog(IMainWindow mw)
        {
            if (mw?.ActiveBookForm != null)
            {
                try
                {
                    if (Icd == null)
                    {
                        Icd = new InsertCellsDialog(mw);
                        Icd.ActiveView = mw.ActiveView;
                        Icd.Show(mw.GetForm());
                    }
                    else
                        Icd.Activate();
                }
                catch (Exception ex)
                {
                    MesBox.ErrorMessage(ex);
                }
            }
            else
                MesBox.ErrorMessage("Could not open InsertCellsDialog");
        }
        internal InsertCellsDialog()
        {
            InitializeComponent();
        }

        private InsertCellsDialog(IMainWindow mw)
        {
            InitializeComponent();
            Owner = mw.GetForm();
            Mw = mw;
            RefreshWbv();
        }

        //事件处理
        //
        private void AddEventHandler()
        {
            if (Mw.ActiveBookForm == null)
                return;
            Mw.FileOperator.DisableUI += DisableDialog;
            Mw.FileOperator.EnableUI += EnableDialog;
            Mw.FileOperator.CellBeginEditInternal += DisableDialog;
            Mw.FileOperator.CellCancelEditInternal += EnableDialog;
            Mw.FileOperator.CellEndEditInternal += EnableDialog;
        }
        private void RemoveEventHandler()
        {
            if (Mw.ActiveBookForm == null) return;
            Mw.FileOperator.DisableUI -= DisableDialog;
            Mw.FileOperator.EnableUI -= EnableDialog;
            Mw.FileOperator.CellBeginEditInternal -= DisableDialog;
            Mw.FileOperator.CellCancelEditInternal -= EnableDialog;
            Mw.FileOperator.CellEndEditInternal -= EnableDialog;
        }
        private void DisableDialog(object sender, EventArgs e)
        {
            if (e is spForm.CellBeginEditEventArgs || e is NoFormLeftEventArgs)
                _disableDialog();
        }
        private void EnableDialog(object sender, EventArgs e)
        {
            if (e is spForm.CellCancelEditEventArgs || e is spForm.CellEndEditEventArgs || e is FormExistEventArgs)
                _enableDialog();
        }
        private void _disableDialog()
        {
            groupBox1.Enabled = false;
            buttonOK.Enabled = false;
            buttonInsert.Enabled = false;
        }
        private void _enableDialog()
        {
            groupBox1.Enabled = true;
            buttonOK.Enabled = true;
            buttonInsert.Enabled = true;
        }

        //工作表刷新
        //
        private bool RefreshWbv()
        {
            ActiveView = Mw.ActiveView;
            return ActiveView != null;
        }


        //窗口功能主体
        //
        //
        private InsertShiftDirection GetSelectedInsertOption()
        {
            if (radioButtonShiftRight.Checked || radioButtonEntCol.Checked)
                return InsertShiftDirection.Right;
            if (radioButtonShiftDown.Checked || radioButtonEntRow.Checked)
                return InsertShiftDirection.Down;
            return InsertShiftDirection.Down;
        }
        private IRange GetSelectedRange()
        {
            if (radioButtonShiftRight.Checked || radioButtonShiftDown.Checked)
                return ActiveView.RangeSelection;
            if (radioButtonEntRow.Checked)
                return ActiveView.RangeSelection.EntireRow;
            if (radioButtonEntCol.Checked)
                return ActiveView.RangeSelection.EntireColumn;
            return ActiveView.RangeSelection;
        }
        private void Init()
        {
            if (!RefreshWbv())
                return;
            ActiveView.GetLock();
            try
            {
                Command insert = new CommandRange.Insert(GetSelectedRange(), GetSelectedInsertOption());
                ActiveView.ActiveCommandManager.Execute(insert);
            }
            finally
            {
                ActiveView.ReleaseLock();
            }
        }

        //按钮及窗体事件
        //
        private void buttonOK_Click(object sender, EventArgs e)
        {
            Init();
            Close();
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            Init();
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void InsertCellsDialog_FormClosed(object sender, FormClosedEventArgs e)
        {
            Icd = null;
            RemoveEventHandler();
        }
        private void InsertCellsDialog_Load(object sender, EventArgs e)
        {
            radioButtonShiftDown.Checked = true;
            AddEventHandler();
        }


    }
}
